package com.henzil.siji;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TextView;

import com.henzil.siji.bean.ResultBean;
import com.henzil.siji.bean.VehiclesInfo;
import com.henzil.siji.bean.Weather;
import com.henzil.siji.data.DriverContentProvider;
import com.henzil.siji.server.QueryVehicleViolation;
import com.henzil.siji.util.DataUtil;
import com.henzil.siji.util.Global;
import com.henzil.siji.weather.Forecast_conditions;
import com.henzil.siji.weather.ReadWeather;

public class NewMainActivity extends Activity {
    /** Called when the activity is first created. */
	
	static final private int ADD_CARINFO = 0;
	static final private int SELECT_CITY = 1;
	static final private int SELECT_CARIMAGE = 2;
	
	private String cityName = "北京";
	
	private ImageView selectImageView;
	
	public static int TOP = 0;
	
	public static int MIDDLE = 1;
	
	public static int BOTTOM = 2;
	
	private static LayoutParams layoutParams = new LayoutParams(462, 389);
	private static LayoutParams layoutParams2 = new LayoutParams(462, 92);
	
	private static LayoutParams layoutParams3 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
	
	View no_cars_layout;
	
	ViewGroup top_weatherInfo_layout;
	ViewGroup middle_weatherInfo_layout;
	ViewGroup bottom_weatherInfo_layout;
	
	View topWeatherInfoView;
	View middleWeatherInfoView;
	View bottomWeatherInfoView;
	
	View topWeatherInfoDetailView;
	View middleWeatherInfoDetailView;
	View bottomWeatherInfoDetailView;
	
	ViewGroup carinfo_layout;
	
	List<CarInfo> carInfos = new ArrayList<CarInfo>();
	
	List<VehiclesInfo> vehiclesInfos = new ArrayList<VehiclesInfo>();
	{
		carInfos.add(new CarInfo("2010-12-24 18:16", "丰台京开高速马甲楼桥北出京", "804008", "未处理", "违反规定在应急车道内行驶的", "0", "200"));
		carInfos.add(new CarInfo("2011-01-10 15:16", "丰台四环路榴乡桥外环", "804008", "未处理", "违反规定在应急车道内行驶的", "0", "200"));
		carInfos.add(new CarInfo("2011-01-26 18:16", "丰台京开高速马甲楼桥北出京", "804008", "未处理", "违反规定在应急车道内行驶的", "0", "200"));
		carInfos.add(new CarInfo("2011-03-30 15:16", "丰台四环路榴乡桥外环", "804008", "未处理", "违反规定在应急车道内行驶的", "0", "200"));
		
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d("tag1", "width>>" 
                + this.getWindow().getWindowManager().getDefaultDisplay() 
                        .getWidth() 
                + " height>>" 
                + this.getWindow().getWindowManager().getDefaultDisplay() 
                        .getHeight());
        intData();
        intView();
    }
	
	//初始化VehiclesInfos信息
	private void intData(){
		vehiclesInfos.clear();
		Cursor cursor = managedQuery(DriverContentProvider.CONTENT_URI, null,
				null, null, null);
		
		while (cursor.moveToNext()) {
			VehiclesInfo vehiclesInfo = new VehiclesInfo();
			vehiclesInfo.setId(cursor.getInt(0));
			vehiclesInfo.setCarLicense(cursor.getString(1));
			vehiclesInfo.setEngineModel(cursor.getString(2));
			vehiclesInfo.setImageId(cursor.getInt(3));
			vehiclesInfos.add(vehiclesInfo);
		}
		cursor.close();
	}
	//初始化View
	private void intView() {
		no_cars_layout = findViewById(R.id.no_cars_layout);
		no_cars_layout.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivityForResult(new Intent(NewMainActivity.this, AddCarActivity.class), ADD_CARINFO);
			}
		});
		//所有车辆信息
		carinfo_layout = (ViewGroup) findViewById(R.id.carinfo_layout);
		
		//TODO
		if(vehiclesInfos.size()<=0){
			no_cars_layout.setVisibility(View.VISIBLE);
			carinfo_layout.setVisibility(View.GONE);
		} else {
			no_cars_layout.setVisibility(View.GONE);
			carinfo_layout.setVisibility(View.VISIBLE);
			setCarsView();
		}
		
		//天气信息视图
		top_weatherInfo_layout = (ViewGroup) findViewById(R.id.top_weatherInfo_layout);
		middle_weatherInfo_layout = (ViewGroup) findViewById(R.id.middle_weatherInfo_layout);
		bottom_weatherInfo_layout = (ViewGroup) findViewById(R.id.bottom_weatherInfo_layout);
		setWeatherView();
		setWeatherData();
		setTopWeatherView();
	}
	//设置当天的天气详情显示时，各个View加载
	private void setTopWeatherView(){
		removeAllView();
		top_weatherInfo_layout.addView(topWeatherInfoDetailView, layoutParams);
		middle_weatherInfo_layout.addView(middleWeatherInfoView, layoutParams2);
		bottom_weatherInfo_layout.addView(bottomWeatherInfoView, layoutParams2);
	}
	//设置明天的天气详情显示时，各个View加载
	private void setMiddleWeatherView(){
		removeAllView();
		top_weatherInfo_layout.addView(topWeatherInfoView, layoutParams2);
		middle_weatherInfo_layout.addView(middleWeatherInfoDetailView, layoutParams);
		bottom_weatherInfo_layout.addView(bottomWeatherInfoView, layoutParams2);
	}
	//设置后天的天气详情显示时，各个View加载
	private void setBottomWeatherView(){
		removeAllView();
		top_weatherInfo_layout.addView(topWeatherInfoView, layoutParams2);
		middle_weatherInfo_layout.addView(middleWeatherInfoView, layoutParams2);
		bottom_weatherInfo_layout.addView(bottomWeatherInfoDetailView, layoutParams);
	}
	//加载天气View
	private void setWeatherView(){
		topWeatherInfoView = getLayoutInflater().inflate(R.layout.weather_info, null);
		topWeatherInfoView.setBackgroundResource(R.drawable.cityinfo_short_1);
		topWeatherInfoView.setTag(TOP);
		
		middleWeatherInfoView = getLayoutInflater().inflate(R.layout.weather_info, null);
		middleWeatherInfoView.setBackgroundResource(R.drawable.cityinfo_short_2);
		middleWeatherInfoView.setTag(MIDDLE);
		
		bottomWeatherInfoView = getLayoutInflater().inflate(R.layout.weather_info, null);
		bottomWeatherInfoView.setBackgroundResource(R.drawable.cityinfo_short_3);
		bottomWeatherInfoView.setTag(BOTTOM);
		
		topWeatherInfoView.setOnClickListener(longOrShortSwithClickListener);
		middleWeatherInfoView.setOnClickListener(longOrShortSwithClickListener);
		bottomWeatherInfoView.setOnClickListener(longOrShortSwithClickListener);
		
		topWeatherInfoDetailView = getLayoutInflater().inflate(R.layout.weather_detail_info, null);
		topWeatherInfoDetailView.setBackgroundResource(R.drawable.cityinfo_long_1);
		
		middleWeatherInfoDetailView = getLayoutInflater().inflate(R.layout.weather_detail_info, null);
		middleWeatherInfoDetailView.setBackgroundResource(R.drawable.cityinfo_long_2);
		
		bottomWeatherInfoDetailView = getLayoutInflater().inflate(R.layout.weather_detail_info, null);
		bottomWeatherInfoDetailView.setBackgroundResource(R.drawable.cityinfo_long_3);
	}
	//删除掉天气所有加载的View
	private void removeAllView(){
		top_weatherInfo_layout.removeAllViews();
		middle_weatherInfo_layout.removeAllViews();
		bottom_weatherInfo_layout.removeAllViews();
	}
	//得到天气数据
	private void setWeatherData(){
		ReadWeather readWeather = new ReadWeather();
		Weather weather = readWeather
				.analysisData(readWeather
						.downData(Global.GOOGLE_URL_WEATHER+cityName));
		if(weather != null){
			setWeatherDataInView(weather);
		}
		
	}
	
	private void setWeatherDataInView(Weather weather){
		Log.v(Global.TAG, ""+weather);
		weather = DataUtil.getAllWeatherInfo(weather);
		
		Log.i(Global.TAG, ""+weather.getForecast_information().getCity().getData());
		Log.i(Global.TAG, ""+weather.getForecast_information().getPostal_code().getData());
		Log.i(Global.TAG, ""+weather.getForecast_information().getLatitude_e6().getData());
		Log.i(Global.TAG, ""+weather.getForecast_information().getLongitude_e6().getData());
		Log.i(Global.TAG, ""+weather.getForecast_information().getForecast_data().getData());
		Log.i(Global.TAG, ""+weather.getForecast_information().getCurrent_data_time().getData());
		Log.i(Global.TAG, ""+weather.getForecast_information().getUnit_system().getData());
		Log.i(Global.TAG, "------------------------------------------------------");
		Log.i(Global.TAG, ""+weather.getCurrent_conditions().getCondition().getData());
		Log.i(Global.TAG, ""+weather.getCurrent_conditions().getTemp_f().getData());
		Log.i(Global.TAG, ""+weather.getCurrent_conditions().getTemp_c().getData());
		Log.i(Global.TAG, ""+weather.getCurrent_conditions().getHumidity().getData());
		Log.i(Global.TAG, ""+weather.getCurrent_conditions().getIcon().getData());
		Log.i(Global.TAG, ""+weather.getCurrent_conditions().getWind_condition().getData());
		Log.i(Global.TAG, "------------------------------------------------------");
		for(Forecast_conditions forecast_conditions : weather.getForecast_conditionses()){
			Log.i(Global.TAG, ""+forecast_conditions.getDay_of_week().getData());
			Log.i(Global.TAG, ""+forecast_conditions.getLow().getData());
			Log.i(Global.TAG, ""+forecast_conditions.getHigh().getData());
			Log.i(Global.TAG, ""+forecast_conditions.getIcon().getData());
			Log.i(Global.TAG, ""+forecast_conditions.getCondition().getData());
			Log.i(Global.TAG, "------------------------------------------------------");
		}
		Log.i(Global.TAG, "------------------------------------------------------");
		Log.i(Global.TAG, ""+weather.getPhoto());
		
		((TextView) topWeatherInfoView.findViewById(R.id.weather_top)).setText(weather.getForecast_conditionses().get(0).getCondition().getData());
		((TextView) topWeatherInfoView.findViewById(R.id.data_top)).setText(weather.getForecast_conditionses().get(0).getDay());
		((TextView) topWeatherInfoView.findViewById(R.id.temperature_top)).setText(weather.getForecast_conditionses().get(0).getLow().getData()+"~"+weather.getForecast_conditionses().get(0).getHigh().getData()+"℃");
		
		((TextView) middleWeatherInfoView.findViewById(R.id.weather_top)).setText(weather.getForecast_conditionses().get(1).getCondition().getData());
		((TextView) middleWeatherInfoView.findViewById(R.id.data_top)).setText(weather.getForecast_conditionses().get(1).getDay());
		((TextView) middleWeatherInfoView.findViewById(R.id.temperature_top)).setText(weather.getForecast_conditionses().get(1).getLow().getData()+"~"+weather.getForecast_conditionses().get(1).getHigh().getData()+"℃");
		
		((TextView) bottomWeatherInfoView.findViewById(R.id.weather_top)).setText(weather.getForecast_conditionses().get(2).getCondition().getData());
		((TextView) bottomWeatherInfoView.findViewById(R.id.data_top)).setText(weather.getForecast_conditionses().get(2).getDay());
		((TextView) bottomWeatherInfoView.findViewById(R.id.temperature_top)).setText(weather.getForecast_conditionses().get(2).getLow().getData()+"~"+weather.getForecast_conditionses().get(2).getHigh().getData()+"℃");
		
		((TextView) topWeatherInfoDetailView.findViewById(R.id.area)).setText(weather.getForecast_information().getPostal_code().getData());
		((TextView) topWeatherInfoDetailView.findViewById(R.id.mouth)).setText(weather.getForecast_conditionses().get(0).getDay());
		((TextView) topWeatherInfoDetailView.findViewById(R.id.week)).setText(weather.getForecast_conditionses().get(0).getDay_of_week().getData());
		((TextView) topWeatherInfoDetailView.findViewById(R.id.weather_display)).setText(weather.getForecast_conditionses().get(0).getCondition().getData());
		((TextView) topWeatherInfoDetailView.findViewById(R.id.temperature)).setText(weather.getForecast_conditionses().get(0).getLow().getData()+"~"+weather.getForecast_conditionses().get(0).getHigh().getData()+"℃");
		Drawable drawable = DataUtil.setWeartherDrawable(this, weather.getForecast_conditionses().get(0).getIcon().getData());
		((ImageView) topWeatherInfoDetailView.findViewById(R.id.weather_image)).setImageDrawable(drawable);
		
		((TextView) middleWeatherInfoDetailView.findViewById(R.id.area)).setText(weather.getForecast_information().getPostal_code().getData());
		((TextView) middleWeatherInfoDetailView.findViewById(R.id.mouth)).setText(weather.getForecast_conditionses().get(1).getDay());
		((TextView) middleWeatherInfoDetailView.findViewById(R.id.week)).setText(weather.getForecast_conditionses().get(1).getDay_of_week().getData());
		((TextView) middleWeatherInfoDetailView.findViewById(R.id.weather_display)).setText(weather.getForecast_conditionses().get(1).getCondition().getData());
		((TextView) middleWeatherInfoDetailView.findViewById(R.id.temperature)).setText(weather.getForecast_conditionses().get(1).getLow().getData()+"~"+weather.getForecast_conditionses().get(1).getHigh().getData()+"℃");
		drawable = DataUtil.setWeartherDrawable(this, weather.getForecast_conditionses().get(1).getIcon().getData());
		((ImageView) middleWeatherInfoDetailView.findViewById(R.id.weather_image)).setImageDrawable(drawable);
		
		((TextView) bottomWeatherInfoDetailView.findViewById(R.id.area)).setText(weather.getForecast_information().getPostal_code().getData());
		((TextView) bottomWeatherInfoDetailView.findViewById(R.id.mouth)).setText(weather.getForecast_conditionses().get(2).getDay());
		((TextView) bottomWeatherInfoDetailView.findViewById(R.id.week)).setText(weather.getForecast_conditionses().get(2).getDay_of_week().getData());
		((TextView) bottomWeatherInfoDetailView.findViewById(R.id.weather_display)).setText(weather.getForecast_conditionses().get(2).getCondition().getData());
		((TextView) bottomWeatherInfoDetailView.findViewById(R.id.temperature)).setText(weather.getForecast_conditionses().get(2).getLow().getData()+"~"+weather.getForecast_conditionses().get(2).getHigh().getData()+"℃");
		drawable = DataUtil.setWeartherDrawable(this, weather.getForecast_conditionses().get(2).getIcon().getData());
		((ImageView) bottomWeatherInfoDetailView.findViewById(R.id.weather_image)).setImageDrawable(drawable);
		
		washSuggestion(weather);
	}
	//洗车建议数据显示
	private void washSuggestion(Weather weather){
		boolean b0,b1,b2,b3;
		b0 = weather.getForecast_conditionses().get(0).getCondition().getData().equals("雨");
		b1 = weather.getForecast_conditionses().get(1).getCondition().getData().equals("雨");
		b2 = weather.getForecast_conditionses().get(2).getCondition().getData().equals("雨");
		b3 = weather.getForecast_conditionses().get(3).getCondition().getData().equals("雨");
		
		if(!b0){
			if(!b1){
				if(!b2){
					((TextView) topWeatherInfoView.findViewById(R.id.weather_forThings_top)).setText(R.string.suitable_for_car_wash);
					((TextView) topWeatherInfoDetailView.findViewById(R.id.weather_forThings)).setText(R.string.suitable_for_car_wash);
					((TextView) topWeatherInfoDetailView.findViewById(R.id.weather_forThings_detail)).setText(R.string.suitable_for_car_wash_detail);
				} else {
					((TextView) topWeatherInfoView.findViewById(R.id.weather_forThings_top)).setText(R.string.more_appropriate_for_the_car_wash);
					((TextView) topWeatherInfoDetailView.findViewById(R.id.weather_forThings)).setText(R.string.more_appropriate_for_the_car_wash);
					((TextView) topWeatherInfoDetailView.findViewById(R.id.weather_forThings_detail)).setText(R.string.more_appropriate_for_the_car_wash_detail);
				}
			} else {
				((TextView) topWeatherInfoView.findViewById(R.id.weather_forThings_top)).setText(R.string.not_washing);
				((TextView) topWeatherInfoDetailView.findViewById(R.id.weather_forThings)).setText(R.string.not_washing);
				((TextView) topWeatherInfoDetailView.findViewById(R.id.weather_forThings_detail)).setText(R.string.not_washing2_detail);
			}
		} else {
			((TextView) topWeatherInfoView.findViewById(R.id.weather_forThings_top)).setText(R.string.not_washing);
			((TextView) topWeatherInfoDetailView.findViewById(R.id.weather_forThings)).setText(R.string.not_washing);
			((TextView) topWeatherInfoDetailView.findViewById(R.id.weather_forThings_detail)).setText(R.string.not_washing_detail);
		}
		if(!b1){
			if(!b2){
				if(!b3){
					((TextView) middleWeatherInfoView.findViewById(R.id.weather_forThings_top)).setText(R.string.suitable_for_car_wash);
					((TextView) middleWeatherInfoDetailView.findViewById(R.id.weather_forThings)).setText(R.string.suitable_for_car_wash);
					((TextView) middleWeatherInfoDetailView.findViewById(R.id.weather_forThings_detail)).setText(R.string.suitable_for_car_wash_detail);
				} else {
					((TextView) middleWeatherInfoView.findViewById(R.id.weather_forThings_top)).setText(R.string.more_appropriate_for_the_car_wash);
					((TextView) middleWeatherInfoDetailView.findViewById(R.id.weather_forThings)).setText(R.string.more_appropriate_for_the_car_wash);
					((TextView) middleWeatherInfoDetailView.findViewById(R.id.weather_forThings_detail)).setText(R.string.more_appropriate_for_the_car_wash_detail);
				}
			} else {
				((TextView) middleWeatherInfoView.findViewById(R.id.weather_forThings_top)).setText(R.string.not_washing);
				((TextView) middleWeatherInfoDetailView.findViewById(R.id.weather_forThings)).setText(R.string.not_washing);
				((TextView) middleWeatherInfoDetailView.findViewById(R.id.weather_forThings_detail)).setText(R.string.not_washing2_detail);
			}
		} else {
			((TextView) middleWeatherInfoView.findViewById(R.id.weather_forThings_top)).setText(R.string.not_washing);
			((TextView) middleWeatherInfoDetailView.findViewById(R.id.weather_forThings)).setText(R.string.not_washing);
			((TextView) middleWeatherInfoDetailView.findViewById(R.id.weather_forThings_detail)).setText(R.string.not_washing_detail);
		}
		if(!b2){
			if(!b3){
				((TextView) bottomWeatherInfoView.findViewById(R.id.weather_forThings_top)).setText(R.string.more_appropriate_for_the_car_wash);
				((TextView) bottomWeatherInfoDetailView.findViewById(R.id.weather_forThings)).setText(R.string.more_appropriate_for_the_car_wash);
				((TextView) bottomWeatherInfoDetailView.findViewById(R.id.weather_forThings_detail)).setText(R.string.more_appropriate_for_the_car_wash_detail);
			} else {
				((TextView) bottomWeatherInfoView.findViewById(R.id.weather_forThings_top)).setText(R.string.not_washing);
				((TextView) bottomWeatherInfoDetailView.findViewById(R.id.weather_forThings)).setText(R.string.not_washing);
				((TextView) bottomWeatherInfoDetailView.findViewById(R.id.weather_forThings_detail)).setText(R.string.not_washing2_detail);
			}
		} else {
			((TextView) bottomWeatherInfoView.findViewById(R.id.weather_forThings_top)).setText(R.string.not_washing);
			((TextView) bottomWeatherInfoDetailView.findViewById(R.id.weather_forThings)).setText(R.string.not_washing);
			((TextView) bottomWeatherInfoDetailView.findViewById(R.id.weather_forThings_detail)).setText(R.string.not_washing_detail);
		}
		
	}
	//天气展示时三天点击事件状态监听
	OnClickListener longOrShortSwithClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			int i = (Integer) v.getTag();
			switch (i) {
			case 0:
				setTopWeatherView();
				break;
			case 1:
				setMiddleWeatherView();
				break;
			case 2:
				setBottomWeatherView();
				break;
			default:
				break;
			}
		}
	};
	
	//切换城市选择
	private void changeCity(){
		Intent intent = new Intent(this, SelectCityActivity.class);
		startActivityForResult(intent, SELECT_CITY);
	}
	
	//设置cars列表View
	private void setCarsView(){
		carinfo_layout.removeAllViews();
		for(int i = 0;i < vehiclesInfos.size();i++){
			VehiclesInfo vehiclesInfo = vehiclesInfos.get(i);
			View carsView = getLayoutInflater().inflate(R.layout.cars, null);
			View cars_layout = carsView.findViewById(R.id.cars_layout);
			cars_layout.setTag(vehiclesInfo);
			cars_layout.setOnLongClickListener(new View.OnLongClickListener() {
				
				@Override
				public boolean onLongClick(View paramView) {
					final VehiclesInfo vehiclesInfo = (VehiclesInfo) paramView.getTag();
					AlertDialog.Builder builder = new AlertDialog.Builder(NewMainActivity.this);
					builder.setItems(new String[]{"编辑本车", "删除本车"}, new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int which) {
	                    	switch (which) {
							case 0:
								Intent intent = new Intent();
								intent.setClass(NewMainActivity.this, AddCarActivity.class);
								Bundle bundle = new Bundle();
								bundle.putInt("vehiclesInfo_id", vehiclesInfo.getId());
								intent.putExtras(bundle);
								startActivityForResult(intent, ADD_CARINFO);
								break;
							case 1:
								String[] strs = {String.valueOf(vehiclesInfo.getId())};
								getContentResolver().delete(DriverContentProvider.CONTENT_URI_VEHICLESINFO_DELETE, DriverContentProvider._ID+"=?", strs);
								refresh();
								break;
							default:
								break;
							}
	                       
	                    }
	                });
	                builder.create().show();;
					return true;
				}
			});
//			carsView.setTag(vehiclesInfo);
			ImageView carAvatar = (ImageView) carsView.findViewById(R.id.carAvatar);
			carAvatar.setImageResource(vehiclesInfo.getImageId()==-1 ? R.drawable.icon_addcar_placeholder : Global.getImageIdList().get(vehiclesInfo.getImageId()));
			carAvatar.setTag(vehiclesInfo);
			carAvatar.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					selectImageView = (ImageView) v;
					VehiclesInfo vehiclesInfo = (VehiclesInfo) v.getTag();
					Intent intent = new Intent();
					intent.setClass(NewMainActivity.this, CarImagesPickerActivity.class);
					Bundle bundle = new Bundle();
					bundle.putInt("vehiclesInfo_id", vehiclesInfo.getId());
					bundle.putInt("imageId", vehiclesInfo.getImageId());
					intent.putExtras(bundle);
					startActivityForResult(intent, SELECT_CARIMAGE);
					
				}
			});
			TextView numberPlate = (TextView) carsView.findViewById(R.id.numberPlate);
			numberPlate.setText(vehiclesInfo.getCarLicense());
			TextView weizhang_num = (TextView) carsView.findViewById(R.id.weizhang_num);
			final ImageView arrow_image = (ImageView) carsView.findViewById(R.id.arrow_image);
			final ViewGroup carinfo_detail_layout = (ViewGroup) carsView.findViewById(R.id.carinfo_detail_layout);
			carinfo_layout.addView(carsView);
			cars_layout.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View paramView) {
					if(carinfo_detail_layout.getVisibility() == View.GONE){
						carinfo_detail_layout.setVisibility(View.VISIBLE);
						arrow_image.setImageResource(R.drawable.arrow_down);
					} else {
						carinfo_detail_layout.setVisibility(View.GONE);
						arrow_image.setImageResource(R.drawable.arrow_right);
					}
				}
			});
			
			View carinfoDetailView = getLayoutInflater().inflate(R.layout.carinfo, null);
			TableLayout tableLayout = (TableLayout) carinfoDetailView.findViewById(R.id.carinfo_table);
			
			View carinfoGoodNewsView = getLayoutInflater().inflate(R.layout.carinfo_goodnews, null);
			//
			List<ResultBean> resultBeans = new ArrayList<ResultBean>();
			resultBeans = new QueryVehicleViolation().queryResult(vehiclesInfo);
			if(resultBeans.size() <= 0){
				weizhang_num.setText("0");
				weizhang_num.setBackgroundResource(R.drawable.carinfo_count_green_backgroud);
				setCarGoodNews(carinfo_detail_layout, carinfoGoodNewsView);
			} else {
				weizhang_num.setText(""+resultBeans.size());
				weizhang_num.setBackgroundResource(R.drawable.carinfo_count_background);
				setTableRow(resultBeans, carinfo_detail_layout, carinfoDetailView, tableLayout);
			}
		}
	}
	
	//设置没有违法信息时，显示的View
	private void setCarGoodNews(ViewGroup viewGroup, View childView){
		
		viewGroup.addView(childView, layoutParams3);
	}
	
	//设置有违法信息时，显示的table
	private void setTableRow(List<ResultBean> resultBeans, ViewGroup viewGroup, View childView, TableLayout tableLayout){
		viewGroup.addView(childView, layoutParams3);
		for(int i=0;i<resultBeans.size();i++){
			CarInfo carInfo = carInfos.get(i);
			ResultBean resultBean = resultBeans.get(i);
			Log.v(Global.TAG, "@@@@@@@@@@@@@@@");
			for(int j=0;j<7;j++){
				Log.v(Global.TAG, "***************");
				View rowView = getLayoutInflater().inflate(R.layout.carinfo_item, null);;
				TextView column0 = (TextView)rowView.findViewById(R.id.number_text);
				TextView column1 = (TextView)rowView.findViewById(R.id.column1);
				TextView column2 = (TextView)rowView.findViewById(R.id.column2);
				switch (j) {
				case 0:
					column0.setText(""+(i+1)+".");
					column1.setText("时间：");
//						column2.setText(carInfo.getTime());
					column2.setText(resultBean.getTime());
					break;
				case 1:
					column0.setVisibility(View.INVISIBLE);
					column1.setText("地点：");
//						column2.setText(carInfo.getArea());
					column2.setText(resultBean.getAddress());
					break;
				case 2:
					column0.setVisibility(View.INVISIBLE);
					column1.setText("违章代码：");
					column2.setText(carInfo.getDaiMa());
					break;
				case 3:
					column0.setVisibility(View.INVISIBLE);
					column1.setText("状态：");
//						column2.setText(carInfo.getStatus());
					column2.setText(resultBean.getStatus());
					break;
				case 4:
					column0.setVisibility(View.INVISIBLE);
					column1.setText("违法行为：");
//						column2.setText(carInfo.getOffense());
					column2.setText(resultBean.getReason());
					break;
				case 5:
					column0.setVisibility(View.INVISIBLE);
					column1.setText("违法记分：");
					column2.setText(carInfo.getIntegral());
					break;
				case 6:
					column0.setVisibility(View.INVISIBLE);
					column1.setText("处罚金额：");
					column2.setText(carInfo.getMoney());
					break;
				default:
					break;
				}
				Log.v(Global.TAG, "###########");
				tableLayout.addView(rowView);
			}
		}
	}
	//刷新操作，用在添加了车辆或者是删除了车辆时调用，以及刷新天气信息
	void refresh(){
		
		intData();
		if(vehiclesInfos.size()<=0){
			no_cars_layout.setVisibility(View.VISIBLE);
			carinfo_layout.setVisibility(View.GONE);
		} else {
			no_cars_layout.setVisibility(View.GONE);
			carinfo_layout.setVisibility(View.VISIBLE);
			setCarsView();
		}
		setCarsView();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 4, 0, "添加车辆").setIcon(android.R.drawable.ic_menu_add);
//		menu.add(0, 1, 1, "切换城市").setIcon(android.R.drawable.ic_menu_preferences);
		menu.add(0, 2, 2, "刷新").setIcon(android.R.drawable.ic_menu_rotate);
//		menu.add(0, 3, 3, "好友推荐").setIcon(android.R.drawable.ic_menu_send);
//		menu.add(0, 5, 4, "提交反馈").setIcon(android.R.drawable.ic_menu_edit);
//		menu.add(0, 6, 5, "关于").setIcon(android.R.drawable.ic_menu_info_details);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case 1:
			changeCity();
			return true;

		case 2:
			refresh();
			return true;
			
		case 3:
			return true;
			
		case 4:
			startActivityForResult(new Intent(NewMainActivity.this, AddCarActivity.class), ADD_CARINFO);
			return true;
			
		case 5:
			return true;
			
		case 6:
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == ADD_CARINFO) {
			if (resultCode == RESULT_CANCELED) {
//				text2.setText("点击了返回");
			} else {
				refresh();
			}
		}
		if (requestCode == SELECT_CITY){
			if (resultCode == RESULT_CANCELED) {
//				text2.setText("点击了返回");
			} else {
				if(data != null)
				{
					cityName = data.getDataString();
				}
				refresh();
			}
		}
		if(requestCode == SELECT_CARIMAGE){
			if (resultCode == RESULT_CANCELED) {
			} else {
				if(data != null)
				{
//					data.getExtras().getInt("vehiclesInfo_id");
					int i = data.getExtras().getInt("imageId");
					selectImageView.setImageResource(i==-1 ? R.drawable.icon_addcar_placeholder : Global.getImageIdList().get(i));
				}
			}
			
		}
	}
	
	public class CarInfo{
		//时间
		private String time;
		//地点
		private String area;
		//违章代码
		private String daiMa;
		//状态
		private String status;
		//违法行为
		private String offense;
		//违法积分
		private String integral;
		//处罚金额
		private String money;
		
		public CarInfo(String time, String area, String daiMa, String status, String offense, String integral, String money){
			this.time = time;
			this.area = area;
			this.daiMa = daiMa;
			this.status = status;
			this.offense = offense;
			this.integral = integral;
			this.money = money;
		}
		public String getTime() {
			return time;
		}
		public void setTime(String time) {
			this.time = time;
		}
		public String getArea() {
			return area;
		}
		public void setArea(String area) {
			this.area = area;
		}
		public String getDaiMa() {
			return daiMa;
		}
		public void setDaiMa(String daiMa) {
			this.daiMa = daiMa;
		}
		public String getStatus() {
			return status;
		}
		public void setStatus(String status) {
			this.status = status;
		}
		public String getOffense() {
			return offense;
		}
		public void setOffense(String offense) {
			this.offense = offense;
		}
		public String getIntegral() {
			return integral;
		}
		public void setIntegral(String integral) {
			this.integral = integral;
		}
		public String getMoney() {
			return money;
		}
		public void setMoney(String money) {
			this.money = money;
		}
	}
}